package cn.gson.crm.controller.dept;

import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.DeptDao;
import cn.gson.crm.model.entity.Dept;

@WebServlet("/dept/form")
public class DeptFormServlet extends BaseServlet {

	/**
	 * 部门创建和修改
	 */
	private static final long serialVersionUID = 1L;
	private DeptDao deptDao = new DeptDao();

	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Dept d = deptDao.getDeptById(1, id);
		// 获取所有部门
		List<Dept> allDeptList = deptDao.getDeptList(1);
		request.setAttribute("d", d);
		//id为空时，为创建新部门，否则为修改
		if(id!=null&&!id.isEmpty()){
			// 获取除了本部门以外的部门列表
			List<Dept> NotContainsSelfAndSonDeptList = deptDao.getDeptListNotContainsSelfAndSon(1, id);
			if(NotContainsSelfAndSonDeptList!=null){
				request.setAttribute("NotContainsSelfAndSonDeptList", NotContainsSelfAndSonDeptList);
			}
		}
		request.setAttribute("allDeptList", allDeptList);
		getServletPathAndForward1(request, "/dept/create_dept.jsp");
		//forward("/dept/create_dept.jsp");
	}

	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String deptName = request.getParameter("deptName");
		String id = request.getParameter("id");
		String deptManager = request.getParameter("deptManager");
		String parentId = request.getParameter("parentDept");
		Dept dept = new Dept();
		dept.setDeptName(deptName);
		dept.setDeptManager(deptManager);
		if (parentId.trim() != null && !parentId.trim().isEmpty()) {
			dept.setParentId(Integer.parseInt(parentId));
		} else {
			dept.setParentId(null);
		}
		if (id == null || id.isEmpty()) {
			// 创建新部门
			if (deptName == null || deptName.isEmpty()) {
				request.setAttribute("erro", "部门名称不能为空！");
				get(request, response);
				return;
				//输入空格不能创建
			}else if(deptName.trim()==null|| deptName.trim().isEmpty()){
				request.setAttribute("erro", "部门名称不能为空！");
				get(request, response);
				return;
			}
			// 判断该部门是否已经存在
			Dept dept1 = deptDao.getDeptByName(deptName);
		
			if (dept1 != null) {
				if(dept1.getStatus()==1){
					request.setAttribute("erro", "该部门已经存在，请重新输入！");
					get(request, response);
					return;
				}else{
					deptDao.changeDept(dept1,1);
				}
			}else{
				deptDao.createDept(dept);
			}
		} else {
			// 修改部门信息
			dept.setId(Integer.parseInt(id));
			if (deptName == null || deptName.isEmpty()) {
				request.setAttribute("erro", "部门名称不能为空！");
				get(request, response);
				return;
			}
			// 判断该部门是否已经存在
			List<Dept> deptList = deptDao.getDeptListNotContainsSelf(1, id);
			if(deptList==null){
				deptDao.changeDept(dept);
			}else{
				for (Dept dept2 : deptList) {
					if (deptName.equals(dept2.getDeptName())) {
						request.setAttribute("erro", "该部门已经存在，请重新输入！");
						get(request, response);
						return;
					}
				}
				deptDao.changeDept(dept);
			}
		}
		if(parentId==null||parentId.isEmpty()){
			redirect("dept");
		}else{
			redirect("dept/son?id="+parentId);
		}
	}

}
